package com.sise.aio和bio和nio.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 通过nio来进行数据的读取
 * @author idea
 * @data 2019/1/20
 */
public class Test2 {

    public static void main(String[] args) throws IOException {
        System.out.println("使用nio来读取数据");
        FileInputStream fileInputStream=new FileInputStream("D:\\test.txt");
        FileChannel fileChannel=fileInputStream.getChannel();
        ByteBuffer byteBuffer=ByteBuffer.allocate(1024);

        //channel将数据读入到buffer里面
        fileChannel.read(byteBuffer);
        //重设值buffer里面的postition和limit
        byteBuffer.flip();
        while(byteBuffer.hasRemaining()){
            byte b=byteBuffer.get();
            System.out.print((char)b);
        }
        fileInputStream.close();

    }
}
